package com.zrrd.mq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class CommonConfig implements ApplicationContextAware {
    /**
     * 设置ReturnCallback  关于发送者的整体回执
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //获取RabbitTemplate
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        //设置ReturnCallback
        //message:消息, replyCode：应答码, replyText：失败原因, exchange：交换机, routingKey：路由键
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            //投递失败的操作......
            log.debug("消息发送失败，交换机向队列转发失败，应答码{}，原因{}，交换机{}，路由键{}，消息{}",
                    replyCode,replyText,exchange,routingKey,message);
            //执行其他业务，比如消息重发...
        });
    }
}
